<template>
  <div class="app-container">
    <div class="right">
      <el-button type="primary" @click="goAddBui('')">添加</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在加载..."
      border
      fit
      highlight-current-row
    >
      <el-table-column prop="title" align="center" label="名称"/>
      <el-table-column label="封面" align="center">
        <template slot-scope="scope">
          <img style="height: 50px" :src="scope.row.cover" class="avatar">
        </template>
      </el-table-column>
      <el-table-column label="是否上架" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.enable?'上架':'下架' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.checkStatus.text}}</span>
          <el-popover
            placement="top-start"
            width="200"
            trigger="hover"
            v-if="scope.row.checkStatus.value=='failed'"
            :content="scope.row.remark">
            <span class='reasonsFailure' slot="reference">
              <a style="color: darkred">?</a>
            </span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="queue" label="排序" align="center"/>
      <el-table-column class-name="status-col" label="操作" align="center">
        <template slot-scope="scope">
          <a style="color: #409EFF;margin-right: 30px" @click="goAddBui(scope.row.id)">编辑 </a>
          <a style="color: red" @click="del(scope.row.id,scope.$index)"> 删除</a>
        </template>
      </el-table-column>
    </el-table>
    <pagination @fetchData="fetchData" :total="total"/>

  </div>
</template>

<script>
  import { list,del } from '@/api/buildingPromotion'
  import pagination from '@/components/Pagination/index'
  import { getToken } from '../../utils/auth'
  export default {
    data() {
      return {
        total:0,
        list: null,
        listLoading: true
      }
    },
    components:{
      pagination
    },
    created() {
      this.fetchData({limit:10,page:0})
    },
    methods: {
      goAddBui(id){
        this.$router.push({name:'addBui',query:id?{id}:{}})
      },
      del(targetId,index){
        this.$confirm('确认删除？')
          .then(_ => {
            this.listLoading=true
            del({token:getToken(),targetId}).then(res=>{
              this.$message.success('删除成功')
              this.list.splice(index,1)
            }).finally(
              this.listLoading=false
            )
          })
          .catch(_ => {});
      },
      goToMsgBui() {
        this.$router.push({ name: 'msgBui' })
      },
      fetchData(data) {
        this.listLoading = true
        list({limit:data.limit,page:data.page,managerId:localStorage.getItem('id')}).then(res => {
          this.list = res.data
          this.total=res.total
        }).finally(()=>{
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.limit=val
        this.fetchData()
        console.log(`每页 ${val} 条`)
      },
      handleCurrentChange(val) {
        this.page=val-1
        this.fetchData()

        console.log(`当前页: ${val}`)
      }
    }
  }
</script>
<style lang="scss" scoped>

</style>
